//购物车中商品数据
//获取保存的uid和总价
let uid = localStorage.getItem('uid');
let allPrice = localStorage.getItem('allPrice');

//选择需要插入数据的div
let address = document.querySelector('.address');
let product = document.querySelector('.product');
let price = document.querySelector('.bottom .price_main');
document.getElementById('buy').innerHTML = allPrice;

//定义一个总的订单数据对象
let order_data = {
    address: '',
    allPrice: allPrice,
}
//地址数据
ajax({
    url: '/user/get_address',
    type: 'get',
    data: uid,
    success: function (data) {
        // console.log(data);
        data = JSON.parse(data);
        if (data != null) {
            //数据不为空时将数据保存在对象中
            order_data.address = data;
            //用模板引擎插入数据
            address.innerHTML = template('address_list', order_data);
            check();
        } else {
            address.innerHTML = template('address_list', order_data);
        }
    },
    error: function (e) {
        console.log(e);
    }
});
//商品数据
ajax({
    url: '/pro/cart_detail',
    type: 'get',
    data: uid,
    success: function (data) {
        data = JSON.parse(data);
        if (data != null) {
            //数据不为空时将他保存在对象中
            order_data.product = data;
            // console.log(order_data.product);
        }
        product.innerHTML = template('product_list', order_data);
    },
    error: function (e) {
        console.log(e);
    }
});

price.innerHTML = template('price_list', order_data);

//选中地址的样式
function check() {
    let divs = document.querySelectorAll('.address>div');
    // console.log(divs);
    for (let div of divs) {
        div.addEventListener('click', function () {
            let div_active = document.querySelector('.address>div.active');
            if (div_active) {
                div_active.classList.remove('active');
            }
            this.classList.add('active');
            msg.innerHTML = '';
        });
    }
}

//进行确认购买，保存订单
function buy() {
    let time = new Date();
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();

    //下单日期
    let timeStr = '' + y + m + d + h + s + mm + s;
    //选中的地址id
    let addressId = document.querySelector('.address>div.active>input');
    if (addressId) {
        //获取到地址的id
        addressId = addressId.value;
        //将地址id保存起来
        localStorage.setItem('address', addressId);
    } else {
        msg.innerHTML = '请选择收货地址';
    }
    //商品数据
    let products = order_data.product;

    if (msg.innerHTML == '') {
        //循环输入数据库
        for (let key in products) {
            let dataStr = `uid=${uid}&pid=${products[key].pid}&aid=${addressId}&pnum=${products[key].pnum}&orderTime=${timeStr}`;
            //插入订单
            ajax({
                url: '/user/order',
                type: 'post',
                data: dataStr,
                success: function (data) {
                    //成功后删除购物车数据
                    ajax({
                        url: '/pro/cart_delete',
                        type: 'get',
                        data: products[key].cart_id,
                        success: function(data) {
                            // console.log(data);
                        },
                        error: function(e){
                            console.log(e);
                        }
                    });
                },
                error: function (e) {
                    console.log(e);
                }
            });
        }
        //将订单时间也就是订单号保存起来
        localStorage.setItem('orderTime', timeStr);
        alert('提交订单成功');
        location.href = '/buy.html';
    }
}